﻿using DB.Interface;
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace ReflectionLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("****************************");

            {
                ////获取Object Assembly
                //Assembly a = typeof(object).Module.Assembly;

                ////获取所有程序集中的类型
                //foreach (Type t in a.GetTypes())
                //{
                //    Console.WriteLine(t.FullName);
                //}
            }
           
            {
                ////获取 String 类型的 全部构造函数
                //// This program lists all the public constructors
                ////// of the System.String class.
                //Type t = typeof(System.String);
                //Console.WriteLine("列出 {0} 所有的 constructors ", t);

                ////ConstructorInfo
                //ConstructorInfo[] ci = t.GetConstructors();
                //Console.WriteLine("------List Constructors ---------");
                //PrintMembers(ci);
            }
      
            {
                //下列示例使用 MemberInfo 列出 System.IO.File 类中成员方法数量，
                //并使用 IsPublic 属性确定类的可见性。
                //使用 MemberInfo、MethodInfo、FieldInfo 或 PropertyInfo 
                //对象获取类型的方法、属性、事件和字段的相关信息。
                //Type t = typeof(System.IO.File);
                //MemberInfo[] memberInfos = t.GetMembers();
                //Console.WriteLine("总共有{0}个 member in {1} ",memberInfos.Length,t.FullName);
                //if (t.IsPublic)
                //{
                //    Console.WriteLine("{0} is public.", t.FullName);
                //}
                //PrintMembers(memberInfos);
            }

            {
                //// Specifies the class.
                //Type t = typeof(System.IO.BufferedStream);
                //Console.WriteLine("Listing all the members (public and non public) of the {0} type", t);

                //// Lists static fields first.
                //FieldInfo[] fi = t.GetFields(BindingFlags.Static |
                //    BindingFlags.NonPublic | BindingFlags.Public);
                //Console.WriteLine("// Static Fields");
                //PrintMembers(fi);

                //// Static properties.
                //PropertyInfo[] pi = t.GetProperties(BindingFlags.Static |
                //    BindingFlags.NonPublic | BindingFlags.Public);
                //Console.WriteLine("// Static Properties");
                //PrintMembers(pi);

                //// Static events.
                //EventInfo[] ei = t.GetEvents(BindingFlags.Static |
                //    BindingFlags.NonPublic | BindingFlags.Public);
                //Console.WriteLine("// Static Events");
                //PrintMembers(ei);

                //// Static methods.
                //MethodInfo[] mi = t.GetMethods(BindingFlags.Static |
                //    BindingFlags.NonPublic | BindingFlags.Public);
                //Console.WriteLine("// Static Methods");
                //PrintMembers(mi);

                //// Constructors.
                //ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Instance |
                //    BindingFlags.NonPublic | BindingFlags.Public);
                //Console.WriteLine("// Constructors");
                //PrintMembers(ci);

                //// Instance fields.
                //fi = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic |
                //    BindingFlags.Public);
                //Console.WriteLine("// Instance Fields");
                //PrintMembers(fi);

                //// Instance properites.
                //pi = t.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic |
                //    BindingFlags.Public);
                //Console.WriteLine("// Instance Properties");
                //PrintMembers(pi);

                //// Instance events.
                //ei = t.GetEvents(BindingFlags.Instance | BindingFlags.NonPublic |
                //    BindingFlags.Public);
                //Console.WriteLine("// Instance Events");
                //PrintMembers(ei);

                //// Instance methods.
                //mi = t.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic
                //    | BindingFlags.Public);
                //Console.WriteLine("// Instance Methods");
                //PrintMembers(mi);
            }

            {
                ////动态加载Dll
                //Assembly assembly = Assembly.Load("DB.Sqlserver");
                //////LoadFile 不会加载依赖项，参数必须是完整路径
                ////Assembly assembly1 = Assembly.LoadFile(@"D:\Visual Studio 2017\Projects\NetCoreLearn\ReflectionLearn\bin\Debug\netcoreapp2.0\DB.Sqlserver.dll"); //需要指定文件完整路径
                ////Assembly assembly2 = Assembly.LoadFrom("DB.Sqlserver.dll");

                ////获取模块信息
                //foreach (var item in assembly.GetModules())
                //{
                //    Console.WriteLine($"{ item.Name}");
                //}

                ////获取类信息
                //foreach (var type in assembly.GetTypes())
                //{
                //    Console.WriteLine($"{ type.Name}");


                //    //获取构造函数
                //    foreach (var item in type.GetConstructors())
                //    {
                //        Console.WriteLine($"{ item.Name}");
                //    }

                //    //获取所有方法
                //    foreach (var item in type.GetMethods())
                //    {
                //        Console.WriteLine($"{ item.Name}");
                //    }
                //    //获取所有属性
                //    foreach (var item in type.GetProperties())
                //    {
                //        Console.WriteLine($"{ item.Name}");
                //    }

                //    //获取所有字段
                //    foreach (var item in type.GetFields())
                //    {
                //        Console.WriteLine($"{ item.Name}");
                //    }

                //    //获取所有特性
                //    foreach (var item in type.GetCustomAttributes())
                //    {
                //        Console.WriteLine($"{ item.ToString()}");
                //    }
                //}

          
            }

            {
                //Assembly assembly = Assembly.Load("DB.Sqlserver");
                //Type typeHelper = assembly.GetType("DB.Sqlserver.SqlserverHelper");

                //var sqlHelper = Activator.CreateInstance(typeHelper);


                //((IDBHelper)sqlHelper).Query();
               
            }

            {
                DateTime dateTime = new DateTime();
                Type type = dateTime.GetType();
                foreach (var property in type.GetProperties())
                {
                    Console.WriteLine(property.Name);
                }
            }

            Console.ReadLine();
        }

        public static void PrintMembers(MemberInfo[] ms)
        {
            foreach (var item in ms)
            {
                Console.WriteLine("{0}{1}", "       ", item);
            }
            Console.WriteLine("----------------------------------");
        }


    }
}
